package buttons;

import processing.core.PApplet;
import controllers.C_TooltipControl;
import controllers.ControlManager;
import utilities.EventListener;
import view.DisplayList;
import buttons.control.C_Button;
import buttons.view.V_Button;

/**************************************
BUTTON CLASS 
serves to encapsulate all button component parts. This includes a controller and a display object.
provides convenience methods for accessing and tweaking one or the other of these components without
needing to access them directly.
***************************************/
public class Button
{
	protected V_Button vButton;
	protected C_Button cButton;
	// constructor for buttons that move (ie: on toolbars):
	public Button(DisplayList v, ControlManager c, String n, float xWidth, float yHeight)
	{
	  generateButtons(v, c, n, xWidth, yHeight);
	}
	// constructor for buttons that don't move around:
	public Button(DisplayList v, ControlManager c, String n, float xWidth, float yHeight, float xPos, float yPos)
	{
	  generateButtons(v, c, n, xWidth, yHeight);
	  vButton.setXLoc(xPos);
	  vButton.setYLoc(yPos);
	}
	public Button(DisplayList v, ControlManager c, String n, float xWidth, float yHeight, PApplet p)
	{
	  generateButtons(v, c, n, xWidth, yHeight, p);
	}
	// constructor for buttons that don't move around:
	public Button(DisplayList v, ControlManager c, String n, float xWidth, float yHeight, float xPos, float yPos, PApplet p)
	{
	  generateButtons(v, c, n, xWidth, yHeight, p);
	  vButton.setXLoc(xPos);
	  vButton.setYLoc(yPos);
	}
	
	/* 
	* this special utility is called by the constructor to actually create the buttons. It is overridden 
	* by subclasses to create whatever unique button is needed.  It uses the concept of 
	* polymorphism to call the proper overridden method and also deal with any special 
	* superclass constructor properties.
	* 
	* There are 2 methods for generateButtons. The first is more commonly used but the second allows for more complex structures
	* to be made based on reference to the PApplet
	* */
	protected void generateButtons(DisplayList v, ControlManager c, String n, float xWidth, float yHeight)
	{
	 vButton = new V_Button(n, xWidth, yHeight);
	 cButton = new C_Button(vButton);
	 if(v != null && c != null) { // button is managed by display and control list
		 addToManagerLists(v, c);
	 }
	}
	protected void generateButtons(DisplayList v, ControlManager c, String n, float xWidth, float yHeight, PApplet p)
	{
	 this.generateButtons(v, c, n, xWidth, yHeight);
	}
	
	// add the proper component to their proper list if that list is provided as non-null
	protected void addToManagerLists(DisplayList v, ControlManager c)
	{
		if(v != null) { // button is managed by displayList
			vButton.addSelfToList(v);
		}
		if(c != null){ // button is managed by controlManager
			cButton.addSelfToList(c);
		}
	}
	
	public C_Button getController()
	{
	 return cButton;
	}
	
	public V_Button getView()
	{
	 return vButton;
	}
	
	public void display(PApplet p)
	{
		vButton.display(p);
	}
	
	public void onInteract(float mouseXLoc, float mouseYLoc, String interID)
	{
	 cButton.onInteract(mouseXLoc, mouseYLoc, interID);
	}
	
	public void setTooltip(C_TooltipControl tooltipController, String tipText)
	{
	 cButton.setTooltip(tooltipController, tipText);
	}
	
	public void setVisibility(boolean val)
	{
	 vButton.setVisibility(val);
	}
	
	public void greyOut(boolean val)
	{
	 vButton.greyOut(val);
	}
	
	public void addSubscriber(EventListener subs) {
	    cButton.addSubscriber(subs);
	}
	
	public void setBID(String val)
	{
		cButton.setBID(val);
	}
}
